<?php


function exportcsv()
{

	header('Content-type: text/csv');
	header('Content-Disposition: attachment; filename="monitorIDs.csv');
	$fname = 'php://output';
	$item = $_POST['download'];
	$fp = fopen($fname,'w');
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	$id = mysql_query("Select rid from projects where name=\"".$_POST["download"]."\"");
	$row = mysql_fetch_array($id);
	$sql_str = "";
	if(mysql_num_rows($id)>1)
	{
		$sql_str = " in (\"".$row[0]."\",";
		$i = 1;
		while($i < mysql_num_rows($id)-1)
		{
			$row = mysql_fetch_array($id);
			$sql_str .= "\"".$row[0]."\",";
			$i++;
		}
		$row = mysql_fetch_array($id);
		$sql_str .= "\"".$row[0]."\")";
	}
	else
	 $sql_str = "=\"".$row[0]."\"";

	$mids = mysql_query("Select ID,monID,status from testredirect where rid".$sql_str);
	$list = array();
	fputcsv($fp, array("ID","mid","status"));
	while ($row2 = mysql_fetch_array($mids))
	{	
		fputcsv($fp, array($row2[0],$row2[1],$row2[2]));	
	}
	mysql_close();
	fclose($fp);
	exit;
}


if(isset($_POST["submit"]))
{
	// check for closing projects
	if(isset($_POST["close"]))
	{
		$box=$_POST["close"];
		for ($i=0; $i < count($box); $i++)
		{ 
		 $db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
		 mysql_select_db("JRtu434", $db);
		 mysql_query("Update projects set state=\"closed\" where name=\"".$box[$i]."\"");	
		}
	}
	if(isset ($_POST["download"]))
	{
		exportcsv();
		
	}	
}
 $db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 mysql_select_db("JRtu434", $db);
 $result = mysql_query("Select projects.ID as ID,name as name,sum(if(status=\"complete\",1,0)) as complete,sum(if(status=\"screenout\",1,0)) as screenout, sum(if(status=\"quotafull\",1,0)) as quotafull
 , sum(if(status=\"short\",1,0)) as short, sum(if(status=\"gate\",1,0)) as gate, sum(if(status is null,1,0)) as empty, sum(if(status=\"complete\",1,0))/(sum(if(status=\"complete\",1,0))+sum(if(status=\"screenout\",1,0)))*100 as IR from testredirect,projects where testredirect.rid=projects.rid and state=\"live\" group by name");

 
?><!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html lang="ja"> 
 
<!-- ==== document header ==== --> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta http-equiv="Content-Style-Type" content="text/css"> 
<meta http-equiv="Content-Script-Type" content="text/javascript"> 
<title>Running Projects</title>
<?php include 'layout.php'; ?> 
</head>
	<body>
		<div id="content">
			<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"><?php
			 echo "<table border='1'>";
			 echo "<td>" . "Close". "</td>";
			 echo "<td>" . "Download". "</td>";
			 echo "<td>" . "Name". "</td>";
			 echo "<td>" ."Complete". "</td>";
			 echo "<td>" ."Screenout" . "</td>";
			 echo "<td>" ."Quotafull" . "</td>";
			 echo "<td>" ."Short" . "</td>";
			 echo "<td>" ."Gate" . "</td>";
			 echo "<td>" ."In Progress" . "</td>";
			echo "<td>" ."IR %" . "</td>";
			echo "</tr>";
			while($row = mysql_fetch_array($result))
			  {
			  echo "<tr><td><input type=\"checkbox\" name=\"close[]\" value=\"".$row['name'] ."\"></td>";
			  echo "<td><input type=\"radio\" name=\"download\" value=\"".$row['name'] ."\"></td>";
			  echo "<td>" . $row['name'] . "&nbsp;&nbsp;&nbsp;</td>";
			  echo "<td>" . $row['complete'] . "</td>";
			  echo "<td>" . $row['screenout'] . "</td>";
			  echo "<td>" . $row['quotafull'] . "</td>";
			  echo "<td>" . $row['short'] . "</td>";
			  echo "<td>" . $row['gate'] . "</td>";
			  echo "<td>" . $row['empty'] . "</td>";
			  echo "<td>" . $row['IR'] . "</td>";
			  echo "</tr>";
			  }
			  echo "</table>";
			  
			 mysql_close();
			?>
			<input type="submit" value="submit" name="submit">
			</form>
		</div>
	</body>
</html>
